// ThreadListenTcp.cpp: implementation of the CThreadListenTcp class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "aremoteserver.h"
#include "ThreadListenTcp.h"
#include "Action.h"
#include "Global.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CThreadListenTcp::CThreadListenTcp()
{

}

CThreadListenTcp::~CThreadListenTcp()
{

}

BOOL CThreadListenTcp::Start()
{
	m_Handle = AfxBeginThread((AFX_THREADPROC)MsgTcpProc, NULL);
	if (m_Handle)
		return true;
	else
		return false;
}

DWORD CThreadListenTcp::MsgTcpProc(LPVOID lpParameter)
{	
	AfxSocketInit();
	CASocket Socket(0);
	Socket.CreateServer(DEFAULT_COMM_PORT, SOMAXCONN);	
	Socket.StartServer(MsgSocketCallBackFunc);
	while(1)
	{
		Sleep(1000);
	}
	return 0;
}

void CALLBACK CThreadListenTcp::MsgSocketCallBackFunc(SOCKET s, int port, const char* IP, DWORD param)
{ 
	CASocket Socket(s, 0);
	CCommParam CommParam;
	if (!Socket.RecvReq(&CommParam))
	{
		Socket.Close();
		return;
	}
	CAction::DoTransMsg(&CommParam);
	Socket.SendRsp(&CommParam);
	Socket.Close();
}